﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.成品出库单.service;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.report;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DevExpress.XtraGrid.Demos.util;
using static DXApplicationTangche.UC.成衣进销存.service.成衣进销存Service;
using System.IO;
using DXApplicationTangche.UC.钉钉;
using DXApplicationTangche.DTO;

namespace DXApplicationTangche.UC.成品出库单
{
    public partial class Form成品出库单确认 : DevExpress.XtraEditors.XtraForm
    {
        private List<StockInOutDetailDTO> StockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutTitleDTO 出库单DTO = new StockInOutTitleDTO();
        private String sessionId;
        private String storeId;
        private String storeName;
        private String inOutType;
        private String remarks;
        private Enum成衣进销存 enum成衣进销存;
        public Form成品出库单确认(String sessionId,String storeId,String storeName,String inOutType, List<StockInOutDetailDTO> StockInOutDetailDTOs)
        {
            InitializeComponent();
            this.sessionId = sessionId;
            this.storeId = storeId;
            this.storeName = storeName;
            this.inOutType = inOutType;
            this.Text = inOutType;
            this.StockInOutDetailDTOs = StockInOutDetailDTOs;
        }
        public Form成品出库单确认(String sessionId, String storeId, String storeName, String inOutType,String remarks, List<StockInOutDetailDTO> StockInOutDetailDTOs)
        {
            InitializeComponent();
            this.sessionId = sessionId;
            this.storeId = storeId;
            this.storeName = storeName;
            this.inOutType = inOutType;
            this.Text = inOutType;
            this.remarks = remarks;
            this.StockInOutDetailDTOs = StockInOutDetailDTOs;
        }
        public Form成品出库单确认(String sessionId, String storeId, String storeName, String inOutType, List<StockInOutDetailDTO> StockInOutDetailDTOs,Enum成衣进销存 enum成衣进销存)
        {
            InitializeComponent();
            this.sessionId = sessionId;
            this.storeId = storeId;
            this.storeName = storeName;
            this.inOutType = inOutType;
            this.Text = inOutType;
            this.StockInOutDetailDTOs = StockInOutDetailDTOs;
            this.enum成衣进销存 = enum成衣进销存;
        }
        private void textBox扫描工牌_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox扫描工牌.Text))
                {
                    try
                    {
                        this.splashScreenManager.ShowWaitForm();
                        this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
                        this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
                        this.出库单DTO =
                            成品出库单Service.getSaveStockInOutDetail(
                                this.sessionId,
                                0,
                                this.inOutType,
                                this.storeId,
                                this.storeName,
                                "5",
                                "顺秋服装厂",
                                this.textBox扫描工牌.Text.Trim()
                                );
                        if (!String.IsNullOrEmpty(this.remarks))
                        {
                            this.出库单DTO = 成品出库单Service.saveStockInOutTitleRemarks(this.sessionId, this.remarks);
                        }
                        DXApplicationTangche.UC.成品出库单.service.出库单导出DTO 出库单导出DTO = new DXApplicationTangche.UC.成品出库单.service.出库单导出DTO(this.出库单DTO, this.StockInOutDetailDTOs);
                        String str = FunctionHelper.JsonSerialization(出库单导出DTO);
                        XtraReport出库单 report = new XtraReport出库单(出库单导出DTO);
                        if (this.enum成衣进销存.Equals(Enum成衣进销存.MTO库存))
                        {
                            report.MTO库存();
                        }
                        else if (this.enum成衣进销存.Equals(Enum成衣进销存.MTS库存))
                        {
                            report.MTS库存();
                        }
                        try
                        {
                            //  上传pdf:
                            string saveDirectoryPath = @"pdf\";
                            //  1.先本地生成文件
                            report.ExportToPdf(saveDirectoryPath + this.出库单DTO.inOutCode + @".pdf");
                            //  2.上传本地文件
                            String re = 成品出库单Service.uploadStockInOutFileByCode(this.出库单DTO.inOutCode, saveDirectoryPath + this.出库单DTO.inOutCode + @".pdf");
                            //  3.删除本地文件
                            File.Delete(saveDirectoryPath + re);
                            //  钉钉
                            钉钉Service.start(new ProcessStartDTO(调拨出库类型Consts.工厂调拨出库, this.出库单DTO, re).build("15940992189", 101381090));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
                        }
                        //  显示
                        ReportPrintTool tool = new ReportPrintTool(report);
                        tool.ShowPreview();
                        this.splashScreenManager.CloseWaitForm();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    this.textBox扫描工牌.Clear();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("请扫描工牌");
                }
            }
        }
    }
}